package com.orangelife.webservice.service.commquery;

import java.lang.reflect.Field;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Response;

import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.session.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Service;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.orangelife.common.AbstractV2TransferService;
import com.orangelife.exception.AppBizException;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.utils.StringUtils;


@Service("webservice_commonQuery")
public class CommonQueryWebSerViceImpl extends AbstractV2TransferService
		implements ICommonQueryWebService {

	@Resource(name = "BaseDao")
	private IBaseDaoService baseDao;
	@Override
	public Response selectDataListByXmlID(HttpServletRequest servletRequest,
			String id, String pageIndex, String pageSize) throws Exception,
			AppBizException {
		
		

		
		
		
		

		if (StringUtils.isBlank(id)) {

			throw new AppBizException(CodeConst.SYS_CODE_3001,
					InfoConst.SYS_INFO_3001);

		}
		baseDao.getSqlSession1();
		
	//	baseDao.getSqlSession1(id).selectList("BillMapper.test");
	
	
		if(id.equals("reshXml")){
			
			/**
			 * 重新加载配置文件
			 */
			WebApplicationContext wac
			 = WebApplicationContextUtils.getWebApplicationContext(
					 servletRequest.getSession().getServletContext()
			   );
			
		
			
			
			
		org.springframework.core.io.Resource[] mapperLocations = new PathMatchingResourcePatternResolver().getResources("com/**/*.xml");
		
			
			
	Configuration c=		baseDao.getSqlSession1().getConfiguration();
			c.getMappedStatementNames();
			
			
			removeConfig(c);
			
			for(org.springframework.core.io.Resource r:mapperLocations){
				
				try{
					
					XMLMapperBuilder xmlMap=new XMLMapperBuilder(r.getInputStream(), c, r.toString(), c.getSqlFragments());
					xmlMap.parse();
				}
				catch(Exception e){
					
					continue;
				}
				
				
				
				
			//	System.out.println(""+r.toString());
			}
				
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			


	}
		
	return this.normalResp();
	}

	private void removeConfig(Configuration configuration) throws Exception {
        Class<?> classConfig = configuration.getClass();
        clearMap(classConfig, configuration, "mappedStatements");
        clearMap(classConfig, configuration, "caches");
        clearMap(classConfig, configuration, "resultMaps");
        clearMap(classConfig, configuration, "parameterMaps");
        clearMap(classConfig, configuration, "keyGenerators");
        clearMap(classConfig, configuration, "sqlFragments");
        clearSet(classConfig, configuration, "loadedResources");
    }
    private void clearMap(Class<?> classConfig, Configuration configuration, String fieldName) throws Exception {
        Field field = classConfig.getDeclaredField(fieldName);
        field.setAccessible(true);
        ((Map) field.get(configuration)).clear();
    }
    private void clearSet(Class<?> classConfig, Configuration configuration, String fieldName) throws Exception {
        Field field = classConfig.getDeclaredField(fieldName);
        field.setAccessible(true);
        ((Set) field.get(configuration)).clear();
    }
    
	
	
}
